﻿using HRMSystem;
using HRMSystem.Util;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;

namespace HRMSystem.DBServices
{
    public class EmployeeService : GenericService<Employee>
    {

        public static List<Employee> GetLoggedEmployeeByUserNamePassword(string userName, string password) 
        {
            try
            {
                return db.Employees.Include("hrm_titles").Where(m => m.USERNAME.Equals(userName) && m.PASSWORD.Equals(password) && m.ISRESIGN == false).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static int CountAllEmployees()
        {
            try
            {
                return db.Employees.Count();
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public static int InsertEmployee(Employee employee)
        {
            try
            {
                db.Employees.Add(employee);
                return db.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
                return 0;
            }
        }

        internal static PagingCollection<Employee> GetPaginatedEmployeeListByPage(int page)
        {
            PagingCollection<Employee> pager = new PagingCollection<Employee>();
            int pagesize = pager.PageSize;
            int offset = pager.PageSize * (page - 1);

            var employees = db.Employees.Include("hrm_titles").Include("hrm_contacts").Where(e => e.ISRESIGN == false).ToList();

            pager.Collection = employees.Skip(offset).Take(pagesize).ToList();
            pager.TotalCount = employees.Count();
            pager.CurrentPage = page;

            return pager;
        }

        internal static PagingCollection<Employee> GetPaginatedSearchedEmployeeListByPage(Employee searche, int page)
        {
            PagingCollection<Employee> pager = new PagingCollection<Employee>();
            int pagesize = pager.PageSize;
            int offset = pager.PageSize * (page - 1);

            var employees = db.Employees.Include("hrm_titles").Include("hrm_contacts").Where
                (e => e.ISRESIGN == false && 
                    (
                        e.FIRST_NAME == searche.FIRST_NAME || 
                        e.LAST_NAME == searche.FIRST_NAME || 
                        e.PREFERRED_NAME == searche.FIRST_NAME ||
                        e.ID == searche.ID //|| e.hrm_contacts.Where(c => c.HOME == searche.hrm_contacts.Single().HOME)
                    )).ToList();

            pager.Collection = employees.Skip(offset).Take(pagesize).ToList();
            pager.TotalCount = employees.Count();
            pager.CurrentPage = page;

            return pager;
        }

        internal static PagingCollection<Employee> GetPaginatedQuickSearchedEmployeeListByPage(string _searchText, int page)
        {
            PagingCollection<Employee> pager = new PagingCollection<Employee>();
            int pagesize = pager.PageSize;
            int offset = pager.PageSize * (page - 1);
            int id = 0;

            try
            {
                id = int.Parse(_searchText);
            }
            catch (Exception)
            {
            }

            var employees = db.Employees.Include("hrm_titles").Include("hrm_contacts").Where
                (e => e.ISRESIGN == false &&
                    (
                        e.FIRST_NAME == _searchText ||
                        e.LAST_NAME == _searchText ||
                        e.PREFERRED_NAME == _searchText ||
                        e.ID == id//|| e.hrm_contacts.Where(c => c.HOME == searche.hrm_contacts.Single().HOME)
                    )).ToList();

            pager.Collection = employees.Skip(offset).Take(pagesize).ToList();
            pager.TotalCount = employees.Count();
            pager.CurrentPage = page;

            return pager;
        }
    }
}
